package org.richin.servlet.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
 * 将Servlet的Request对象获取的请求中的HTML特殊字符进行转换
 * @author Administrator
 *
 */
public class ConvertSpecialCharacterServletRequestWrapper extends HttpServletRequestWrapper {

	public ConvertSpecialCharacterServletRequestWrapper(HttpServletRequest request) {
		super(request);
	}
	public String toHtml(String str) {
		if (str == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		int len = str.length();
		for (int i = 0; i < len; i++) {
			char c = str.charAt(i);
			switch (c) {
			case ' ':
				sb.append("&nbsp;");
				break;
			case '<':
				sb.append("&lt;");
				break;
			case '>':
				sb.append("&gt;");
				break;
			case '&':
				sb.append("&amp;");
				break;
			case '"':
				sb.append("&#34;");
				break;
			case '\\':
				sb.append("&#92;");
				break;
			default:
				sb.append(c);
			}
		}
		return sb.toString();
	}

	@Override
	public String getParameter(String name) {
		String value = super.getParameter(name);
		if (null != value) {
			return toHtml(value.trim());
		} else {
			return null;
		}
	}

}
